package com.tbs.easyhomemoneyserver.dao

import com.tbs.easyhomemoneyserver.DO.HomeMember
import com.tbs.easyhomemoneyserver.dao.providers.HomeMemberProvider
import com.tbs.easyhomemoneyserver.model.dto.HomeMemberDTO
import org.apache.ibatis.annotations.Mapper
import org.apache.ibatis.annotations.Select
import org.apache.ibatis.annotations.SelectProvider
import tk.mybatis.mapper.common.BaseMapper

@Mapper
interface HomeMemberDao : BaseMapper<HomeMember> {
    @SelectProvider(type = HomeMemberProvider::class, method = "selectInRoles")
    fun listByHomeIdAndRole(homeId: Long, roles: Collection<String>): List<HomeMemberDTO>

    @Select(
        "select hm.id as id ," +
                "hm.home_id as homeId," +
                "hm.enable as enabled," +
                " hm.member_type as type," +
                "hm.child_id as userId," +
                "u.name as userName " +
                "from home_member hm " +
                "left join sys_user u " +
                "on u.id=hm.child_id " +
                "where hm.home_id=#{homeId}"
    )
    fun listByHomeId(homeId: Long): List<HomeMemberDTO>

    @Select(
        "select hm.id as id ," +
                "hm.home_id as homeId," +
                "hm.enable as enabled," +
                " hm.member_type as type," +
                "hm.child_id as userId," +
                "u.name as userName " +
                "from home_member hm " +
                "left join sys_user u on " +
                "u.id= hm.child_id where " +
                "hm.home_id=#{homeId} " +
                "and hm.child_id=#{userId}"
    )
    fun listByHomeIdAndUserId(homeId: Long, userId: Long): List<HomeMemberDTO>
}
